import {
  API_URL,
  PICTURE_BASE_USR
} from '../../common/common';

export function request(url, method, body) {
  return new Promise((resolve) => {
    wx.request({
      url: API_URL + url,
      data: body,
      method: method,
      header: {
        'content-type': 'application/json',
        'Authorization': getApp().globalData.userInfo.token
      },
      success(res) {
        if (res.statusCode == 200 || res.statusCode == 201 || res.statusCode == 204) {
          resolve(res.data);
        }
      },
      fail(err) {
        reject(err);
      }
    })
  });
}

export function upload(tempFilePath) {
  return new Promise((resolve) => {
    wx.uploadFile({
      url: API_URL + '/file/upload',
      filePath: tempFilePath,
      name: 'file',
      header: {
        'Authorization': getApp().globalData.userInfo.token
      },
      success(res) {
        resolve(PICTURE_BASE_USR + res.data.replace(/"/g, ''));
      }
    })
  });
}